﻿$(document).ready(function () {
    //if the current user is a sales man
    if ($("#IsSalesMan").val() == "true") {
        $("#SalesManFilterContainer").show();
        $("#ShowOnlySalesManAlbums").prop("checked", true);
        $("#ShowOnlySalesManAlbums").prop("disabled", true);
    }

    function bindAlbums(page) {
        $("#tblAlbums tr:not(:first)").remove();
        $("#tblAlbums").hide();

        $("#tblAlbums").after("<div class='loadingImage'></div>");

        var filterData = {
            "Page": page,
            "OrderBy": $("#OrderBy").val(),
            "ShowRemoved": $("#ShowRemoved").prop("checked"),
            "ShowOnlySalesManAlbums": $("#ShowOnlySalesManAlbums").prop("checked")
        };

        //Invoke AJAX method to get the albums list
        $.post(pathPrefix + "Albums/AlbumsList", filterData, function (result) {
            if (result.Albums.length > 0) {
                $("#tblAlbums").show();
            }

            $("#albumTemplate").tmpl(result.Albums).appendTo("#tblAlbums");

            //insert the pages links (if there is more than one)
            if (result.Pages > 1) {
                insertPages(page, result.Pages);
                $("#pagesContainer").show();
            }
            else {
                $("#pagesContainer").hide();
            }

            $("#tblAlbums").siblings(".loadingImage").remove();

            tableAltRows();

            //loop through the "albumStatus" cells
            $("#tblAlbums td[data-album-status-id]").each(function () {
                //preset the status as the value of the DDL
                var albumStatusId = $(this).data("album-status-id");
                $(this).find("select").val(albumStatusId);
            });

            //loop through the "albumLockStatus" cells
            $("#tblAlbums td[data-album-lock-status-id]").each(function () {
                //preset the lock status as the value of the DDL
                var albumLockStatusId = $(this).data("album-lock-status-id");
                $(this).find("select").val(albumLockStatusId);
            });

            //loop through the "salesMan" cells
            $("#tblAlbums td[data-sales-man-id]").each(function () {
                //preset the sales man as the value of the DDL
                var salesManId = $(this).data("sales-man-id");
                $(this).find("select").val(salesManId);
            });
        }, "json");
    }

    $("#OrderBy,#ShowRemoved,#ShowOnlySalesManAlbums").change(function () {
        bindAlbums(1);
    });

    bindAlbums(1);

    function insertPages(currentPage, pages) {
        $("#pagesContainer").html("<div>עבור לעמוד:</div>");

        //get the number of pages and set link for each of these
        for (var i = 1; i <= pages; i++) {
            //current page
            if (i == currentPage) {
                var page = "<div class='bold'>" + i + "</div>";
            }
            //other page
            else {
                var page = "<a href='' data-page='" + i + "'>" + i + "</a>";
            }
            $("#pagesContainer").append(page);
        }
    }

    //when clicking on a page's link - bind the masks table with this page
    $("#pagesContainer a").live("click", function () {
        var page = $(this).data("page");

        bindAlbums(page);

        return false;
    });

    //when the user changes one of the input elements in a row
    $("tr :input").live("change", function () {
        var albumRow = $(this).parents("tr");

        //get all the album data
        var data = albumRow.find(":input").serialize();

        //Invoke AJAX method to update this album (insert or edit)
        $.post(pathPrefix + "Albums/Update", data, function (result) {
            albumRow.find(".successCell img").fadeIn(1000).delay(1000).fadeOut();
        }, "json");
    });

    //when the user change the LockStatus
    $("select[name='AlbumLockStatusId']").live("change", function () {
        var albumLockStatus = $(this).parents("td").data("album-lock-status-id");
        var newLockStatus = $(this).val();

        //if the current lockStatus is "Locked" and the updated lockStatus is "Open"
        if (albumLockStatus == "1" && newLockStatus == "2") {
            //change the status to "בעריכה"
            var row = $(this).parents("tr");
            row.find("[name='AlbumStatusId']").val("3").change();
        }
    });

    var rowToDelete;
    var rowToDeleteData;

    /* Remove Albums
    -----------------*/

    //when the user click the "remove" button
    $(".setAlbumAsRemoved").live("click", function () {
        rowToDeleteData = $(this).tmplItem().data;
        rowToDelete = $(this).parents("tr");

        var mainDialog = updateDialog("הסרת אלבום מהמאגר", "160", "450");

        //Invoke AJAX method to show the dialog for "Remove Album"
        $.get(pathPrefix + "Albums/SetAlbumAsRemoved", { "id": rowToDeleteData.AlbumId }, function (result) {
            mainDialog.html(result);
        }, "html");

        return false;
    });

    $("#SetAlbumAsRemovedWithFiles").live("click", function () {
        SetAlbumAsRemoved(true);
    });

    $("#SetAlbumAsRemovedWithoutFiles").live("click", function () {
        SetAlbumAsRemoved(false);
    });

    function SetAlbumAsRemoved(DeleteFiles) {
        updateDialog("הסרת אלבום מהמאגר", "160", "450");

        //Invoke AJAX method to set this album as removed
        $.post(pathPrefix + "Albums/SetAlbumAsRemoved", { "id": rowToDeleteData.AlbumId, "DeleteFiles": DeleteFiles }, function () {
            rowToDelete.remove();
            closeDialog();
        }, "json");
    }

    $("#CloseRemoveAlbumDialog").live("click", function () {
        closeDialog();
    });
});